﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;

namespace Huirui.Cavan.Application.MainBoundedContext.PurchaseModule
{
    public class UnPayAppService : BaseAppService<UnPay, long, UnPaySearch, IUnPayRepository>, IUnPayAppService
    {
        private ISupplierAppService SupplierAppService { get; set; }

        public override BoolGenericResult<UnPay> AddOrUpdate(UnPay entity)
        {
            Init();
            this.SupplierAppService = this.SpringContext.GetObject(SystemModuleConst.SUPPLIERAPPSERVICE) as ISupplierAppService;

            BoolGenericResult<UnPay> result = new BoolGenericResult<UnPay>();

            if (entity.Supplier.IsNotNull() && entity.Supplier.Id.IsNotEmpty())
            {
                entity.Supplier = this.SupplierAppService.GetById(entity.Supplier.Id);
            }

            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            Administrator administrator = new Administrator(employee);

            result = administrator.AddSupplierInitialBalance(entity.CreateDate, entity.UnPayType, entity.Cost, entity.Currency, entity.CurrencyRate, entity.Supplier);

            if (result.IsSuccess)
            {
                base.AddOrUpdate(result.Object);
            }

            result.Messages.Add("保存成功");

            return result;
        }
    }
}
